package com.jams.goodox.utils;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;

/**
 * Created by jams on 2017/5/2.
 * ***
 * Tel: ***
 */

public class DeviceUtils {
    //强制打开键盘
    public static void openKeyboardForce(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    //关闭键盘
    public static void closeKeyBoard(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    //关闭键盘
    public static void closeKeyBoard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

    //获取屏幕高度
    public static int getDisplayHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getApplicationContext().getResources().getDisplayMetrics();
        int screenHeight = dm.heightPixels;
        return screenHeight;
    }

    //获取屏幕宽度
    public static int getDisplayWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getApplicationContext().getResources().getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        return screenWidth;
    }

    /**
     * 获取设备唯一标识
     *
     * @return
     */
    public static String getUUID() {
        try {
            TelephonyManager tm = (TelephonyManager) TownApplicationLike.getInstance().getSystemService(Context.TELEPHONY_SERVICE);

            String DEVICE_ID = tm.getDeviceId();
            DEVICE_ID = (DEVICE_ID == null ? "" : DEVICE_ID);
            return DEVICE_ID;
        }catch (Exception e){
            e.printStackTrace();
            return "65536";
        }

    }

    //获取app版本号
    public static int getVersionCode() {
        try {
            return TownApplicationLike.getInstance().getPackageManager().getPackageInfo(TownApplicationLike.getInstance().getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static String getVersionName() {
        try {
            return TownApplicationLike.getInstance().getPackageManager().getPackageInfo(TownApplicationLike.getInstance().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static int getStatusBarHeight(Context mActivity) {
        Resources resources = mActivity.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    //复制文字到系统粘贴板
    public static void copyToClipBoard(Activity activity, String content) {
        ClipboardManager myClipboard;
        myClipboard = (ClipboardManager) (activity.getSystemService(Context.CLIPBOARD_SERVICE));
        ClipData myClip;
        myClip = ClipData.newPlainText("text", content);
        myClipboard.setPrimaryClip(myClip);
        ToastUtil.showLong(activity, activity.getString(R.string.copy_success));
    }

    //判断是否安装了应用
    public static boolean checkApkExist(Context context, String packageName){
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
